package controller

import (
	"EnjoyWorking/model"
	"EnjoyWorking/utils"
	"EnjoyWorking/web/service"
	"github.com/kataras/golog"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/hero"
	"strconv"
	"strings"
)

func NewWprItemController(app *iris.Application, party iris.Party) {
	var (
		user       = party.Party("/wprItem")
		controller = WprItem{service: service.WprItem{}}
	)

	user.Post("/create", hero.Handler(controller.create)).Name = "创建工作计划总结项"
	user.Get("/getAll", hero.Handler(controller.getAll)).Name = "获取所有工作计划总结项"
	user.Get("/get", hero.Handler(controller.get)).Name = "获取单个工作计划总结项"
	user.Put("/update", hero.Handler(controller.update)).Name = "编辑工作计划总结项"
	user.Delete("/delete", hero.Handler(controller.delete)).Name = "删除工作计划总结项"

	user.Get("/getByParentID", hero.Handler(controller.getByParentID)).Name = "获取工作计划总结所有项"

	list := app.GetRoutes()
	for _, v := range list {
		if v.Method == "OPTIONS" {
			continue
		}
		if strings.Index(v.Path, "/api/wprItem") != -1 {
			v.Description = "工作计划总结项管理接口"
		}
	}
}

type WprItem struct {
	service service.WprItem
}

func (c *WprItem) create(ctx iris.Context) {
	var (
		code utils.Code
		err  error
		vo = new(model.WprItem)
	)

	if err = ctx.ReadJSON(&vo); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if err = utils.Validator().Struct(vo); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if err = c.service.Insert(vo); err != nil {
		goto ERR
	}

	utils.Ok_(ctx, utils.CODE_WPRITEM_CREATE_OK)
	return
ERR:
	golog.Errorf("工作计划总结项[%s]创建失败。原因：%s，错误：%s", vo.ParentID, code, err)
	utils.InternalServerError(ctx, code, err)
}

func (c *WprItem) getAll(ctx iris.Context) {
	var (
		err    error
		code   utils.Code
		total  int64
		result []*model.WprItem
		resultCopy []*model.WprItemCopy
	)

	if total, result, err = c.service.FindList(); err != nil {
		goto ERR
	}

	for _, v := range result {
		resultCopy = append(resultCopy, &model.WprItemCopy{*v})
	}

	utils.PaginationTableData(ctx, total, resultCopy)
	return
ERR:
	golog.Errorf("失败。原因：%s，错误：%s", code.String(), err)
	utils.InternalServerError(ctx, code, err)
}

func (c *WprItem) getByParentID(ctx iris.Context) {
	var (
		err    error
		code   utils.Code
		total  int64
		result []*model.WprItem
		resultCopy []*model.WprItemCopy
		uid        int64
	)

	if uid, err = ctx.URLParamInt64("id"); err != nil {
		goto ERR
	}

	if total, result, err = c.service.FindByParentID(uint(uid)); err != nil {
		goto ERR
	}

	for _, v := range result {
		resultCopy = append(resultCopy, &model.WprItemCopy{*v})
	}

	utils.PaginationTableData(ctx, total, resultCopy)
	return
ERR:
	golog.Errorf("失败。原因：%s，错误：%s", code.String(), err)
	utils.InternalServerError(ctx, code, err)
}

func (c *WprItem) get(ctx iris.Context) {
}

func (c *WprItem) update(ctx iris.Context) {
	var (
		code utils.Code
		err  error
		item = new(model.WprItem)
	)

	if err = ctx.ReadJSON(&item); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if err = utils.Validator().Struct(item); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if err = c.service.UpdateOne(item); err != nil {
		goto ERR
	}
	utils.Ok_(ctx, utils.CODE_OK)
	return
ERR:
	golog.Errorf("更新工作计划总结项[%d]信息失败。原因：%s，错误：%s", item.ID, code.String(), err)
	utils.InternalServerError(ctx, code, err)
}

func (c *WprItem) delete(ctx iris.Context) {
	var (
		code utils.Code
		err  error
		ids  []int64
	)
	if ids, err = func() (result []int64, err error) {
		idStr := strings.Split(ctx.URLParam("ids"), ",")
		for _, v := range idStr {
			var id int64
			if id, err = strconv.ParseInt(v, 10, 64); err != nil {
				code = utils.CODE_SYS_PARSE_PARAMS_ERROR
				return nil, err
			}
			result = append(result, id)
		}
		return
	}(); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if err = c.service.Delete(ids); err != nil {
		goto ERR
	}

	utils.Ok_(ctx, utils.CODE_OK)
	return
ERR:
	golog.Errorf("操作失败。原因：%s，错误：%s", code.String(), err)
	utils.InternalServerError(ctx, code, err)
}

